package com.hui.server.controller;


import com.hui.server.entity.Position;
import com.hui.server.service.PositionService;
import com.hui.server.util.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *     职位CRUD控制器      设置了路径权限，无特殊情况不需要再单独设置接口的权限，简单增删改查
 * @author hui
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/system/basic/pos")
@Slf4j
public class PositionController {

    private PositionService positionService;

    @Autowired
    public PositionController(PositionService positionService) {
        this.positionService = positionService;
    }

    @ApiOperation(value = "添加职位信息")
    @PostMapping("/")
    public Result<?> addPosition(@RequestBody Position position){
        log.info("进入添加职位接口，需要新增的职位信息：{}",position);
        boolean save = positionService.save(position);
        return save?
                Result.success("添加成功！")
                :Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"添加失败！");
    }

    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/{id}")
    public Result<?> deletePositionById(@PathVariable Integer id){
        log.info("进入删除职位接口，需要删除的职位ID：{}",id);
        boolean remove = positionService.removeById(id);
        return remove?
                Result.success("删除成功！")
                :Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"删除失败！");
    }


    @ApiOperation(value = "批量删除职位信息")
    @DeleteMapping("/")
    public Result<?> deletePositionByIds(Integer[] ids){
        log.info("进入批量删除职位接口，需要删除的职位数量：{}",ids.length);
        boolean remove = positionService.removeByIds(Arrays.asList(ids));
        return remove?
                Result.success("批量删除成功！")
                :Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"批量删除失败！");
    }


    @ApiOperation(value = "修改职位信息")
    @PutMapping("/")
    public Result<?> updatePosition(@RequestBody Position position){
        log.info("进入修改职位接口，需要改为的职位：{}",position);
        boolean update = positionService.updateById(position);
        return update?
                Result.success("修改成功！")
                :Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"修改失败！");
    }


    @ApiOperation(value = "获取职位信息")
    @GetMapping("/")
    public Result<?> getAllPosition(){
        log.info("进入查询所有职位信息接口");
        List<Position> list = positionService.list();
        return Result.success(list);
    }

}
